package com.ymatou.iapi.userregister.testcase;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.GeneralSecurityException;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.spec.InvalidKeySpecException;
import java.util.Base64;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.userregister.parameter.ActivateCodeBean;
import com.ymatou.iapi.userregister.parameter.SendToEmailBean;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.ActivateCodeCall;
import com.ymatou.iapi.userregister.service.SendToEmailCall;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.core.tool.RSAUtils;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.ValidateCodeServiceV2;
import com.ymttest.database.model.ValidationCode;
import com.ymttest.database.model.YmtValidationCode;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.database.sqlwapper.YmtValidationCodeWapper;
import com.ymttest.database.sqlwapper.YmtValidationCodeWapperV2;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;

/**
 * 邮件激活 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_ActivateCode {
	private static ActivateCodeBean ActivateCodeBean;
	private static ActivateCodeCall ActivateCodeCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("邮件激活返回地址");
	}

	@Before
	public void caseUp() {
		ActivateCodeBean = new ActivateCodeBean();
		ActivateCodeCall = new ActivateCodeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(ActivateCodeCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ActivateCode_001() {
		Logger.start(true, "邮件激活返回成功");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			regbean.setLoginMail(TestDataManager.getEmail());
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
//			GetValidateCodeBean getValidateCodeBean = ValidateCodeServiceV2.setGetValidateCodeBean(regbean.getLoginMail());
//			String activeCode = ValidateCodeServiceV2.GetValidateCode(getValidateCodeBean).getValidateCode();

//			String id = getEncodeId(activeCode, regbean.getLoginMail(), userId);
			ActivateCodeBean.setId(regbean.getLoginMail());
			ActivateCodeCall.setData(ActivateCodeBean);
			ActivateCodeCall.callService();
			Logger.verifyIsContains("http://www.ymatou.com/register?confirmation=", ActivateCodeCall.getReturnData(), "验证ReturnData");
			String confirmation=ActivateCodeCall.getReturnData().substring(ActivateCodeCall.getReturnData().indexOf("=")+1);
			
			String msg=RSAUtils.decryptString(FormatUtil.urlDecode(confirmation), EnvSetup.getData("REG_PRIVATE_KEY"));
			Logger.debug(msg);
			String[] s = msg.split("\\|");
			ValidationCode vc = new YmtValidationCodeWapperV2().getValidateData(regbean.getLoginMail());
			Logger.verifyEquals(vc.getCode(),s[0], "验证验证码sValidateCode");
			Logger.verifyEquals(regbean.getLoginMail(), s[1], "验证email");
			Logger.verifyEquals(userId.toString(), s[2], "验证uid");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_ActivateCode_002() {
		Logger.start(false, "不输入id,失败");
		try {
			ActivateCodeCall.setData(ActivateCodeBean);
			ActivateCodeCall.callService();
			Logger.verifyEquals("email is empty", ActivateCodeCall.getReturnData(), "验证ReturnData");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}